//
//  LoginViewController.m
//  UserUse
//
//  Created by angra on 16/10/12.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "ForgetPasswordViewController.h"
#import "LLUserModel.h"
#import "HomePageViewController.h"
#import "BaseNavigationController.h"
#import "LLModificationViewController.h"

@interface LoginViewController ()
@property (nonatomic,weak)UIButton *loginBtn;
@property (nonatomic,weak)UITextField *phoneTF;
@property (nonatomic,weak)UITextField *passwordTF;


@end

@implementation LoginViewController

#pragma mark lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [self setInterface];
    [self getToken];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)setInterface
{
    UIImageView *iconImage = [[UIImageView alloc]init];
    iconImage.image = [UIImage imageNamed:@"logo"];
    iconImage.layer.cornerRadius = 5;
    iconImage.clipsToBounds = YES;
    [self.view addSubview:iconImage];
    [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(kScaleNum(72), kScaleNum(72)));
        make.top.equalTo(self.view).with.offset(kScaleNum(207/2));
    }];
    
    UIImageView *phoneImage = [[UIImageView alloc]init];
    phoneImage.image = [UIImage imageNamed:@"cellphonenumber"];
    [self.view addSubview:phoneImage];
    [phoneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImage.mas_bottom).with.offset(kScaleNum(70));
        make.size.mas_equalTo(CGSizeMake(kScaleNum(10), kScaleNum(16)));
        make.left.equalTo(self.view).with.offset(kScaleNum(60));
    }];
    
    UITextField *phoneTF = [[UITextField alloc]init];
    phoneTF.placeholder =@"请输入手机号";
    phoneTF.tag = 200;
    [phoneTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    phoneTF.keyboardType = UIKeyboardTypePhonePad;
    phoneTF.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:phoneTF];
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneImage.mas_right).with.offset(15);
        make.height.equalTo(phoneImage);
        make.right.equalTo(self.view).with.offset(-kScaleNum(60));
        make.bottom.equalTo(phoneImage.mas_bottom).with.offset(2);
    }];
    _phoneTF = phoneTF;
    
    UIView *lineView1 = [[UIView alloc]init];
    lineView1.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
    [self.view addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneImage.mas_left);
        make.right.equalTo(phoneTF.mas_right);
        make.height.equalTo(@1);
        make.top.equalTo(phoneImage.mas_bottom).with.offset(kScaleNum(5));
    }];
    
    
    UIImageView *passwordImage = [[UIImageView alloc]init];
    passwordImage.image = [UIImage imageNamed:@"password"];
    [self.view addSubview:passwordImage];
    [passwordImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView1.mas_bottom).with.offset(kScaleNum(20));
        make.size.mas_equalTo(CGSizeMake(kScaleNum(12), kScaleNum(16)));
        make.left.equalTo(self.view).with.offset(kScaleNum(60));
    }];
    
    UITextField *passwordTF = [[UITextField alloc]init];
    [passwordTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    passwordTF.tag = 201;
    passwordTF.placeholder = @"请输入密码";
    passwordTF.secureTextEntry = YES;
    passwordTF.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:passwordTF];
    [passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordImage.mas_right).with.offset(15);
        make.height.equalTo(passwordImage);
        make.right.equalTo(self.view).with.offset(-kScaleNum(60));
        make.bottom.equalTo(passwordImage.mas_bottom).with.offset(2);
    }];
    _passwordTF = passwordTF;
    
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
    [self.view addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordImage.mas_left);
        make.right.equalTo(passwordTF.mas_right);
        make.height.equalTo(@1);
        make.top.equalTo(passwordImage.mas_bottom).with.offset(kScaleNum(5));
    }];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [loginBtn setTitleColor:[UIColor colorWithHexString:@"cccccc"] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    loginBtn.backgroundColor = [UIColor whiteColor];
    loginBtn.layer.borderWidth = 1;
    loginBtn.layer.borderColor = [UIColor colorWithHexString:@"cccccc"].CGColor;
    loginBtn.layer.cornerRadius = kScaleNum(30)/2;
    loginBtn.clipsToBounds = YES;
    loginBtn.userInteractionEnabled = NO;
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView2.mas_bottom).with.offset(kScaleNum(21));
        make.left.equalTo(lineView2.mas_left);
        make.right.equalTo(lineView2.mas_right);
        make.height.equalTo(@(kScaleNum(30)));
    }];
    _loginBtn = loginBtn;
    
    UIButton *forgetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetBtn addTarget:self action:@selector(forgetPasswordClick) forControlEvents:UIControlEventTouchUpInside];
    [forgetBtn setTitleColor:[UIColor colorWithHexString:@"cccccc"] forState:UIControlStateNormal];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.view addSubview:forgetBtn];
    [forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(loginBtn.mas_left);
        make.top.equalTo(loginBtn.mas_bottom).with.offset(kScaleNum(15));
        make.width.equalTo(@48);
        make.height.equalTo(@11);
    }];
    
    UIButton *registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registBtn addTarget:self action:@selector(jumpToRegist) forControlEvents:UIControlEventTouchUpInside];
    [registBtn setTitle:@"注册账号" forState:UIControlStateNormal];
    [registBtn setTitleColor:[UIColor colorWithHexString:@"ff9000"] forState:UIControlStateNormal];
    registBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.view addSubview:registBtn];
    [registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(loginBtn.mas_right);
        make.top.equalTo(loginBtn.mas_bottom).with.offset(kScaleNum(15));
        make.width.equalTo(@48);
        make.height.equalTo(@11);
    }];
  
    
}
-(void)textFieldDidChange:(UITextField *)textField
{
    
    UITextField *phoneTF = [self.view viewWithTag:200];
    UITextField *password = [self.view viewWithTag:201];
    
    if ([phoneTF.text checkTel] && [password.text checkPassword]) {
        self.loginBtn.backgroundColor = [UIColor colorWithHexString:@"ff9a02"];
        [self.loginBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        self.loginBtn.layer.borderColor = kClearColor.CGColor;
        self.loginBtn.userInteractionEnabled = YES;
    }
    else
    {
        self.loginBtn.backgroundColor = kWhiteColor;
        [self.loginBtn setTitleColor:[UIColor colorWithHexString:@"cccccc"] forState:UIControlStateNormal];
        self.loginBtn.layer.borderColor = [UIColor colorWithHexString:@"cccccc"].CGColor;
        self.loginBtn.userInteractionEnabled = NO;
    }
    
}
-(void)jumpToRegist
{
    RegistViewController *registVC = [[RegistViewController alloc]init];
    [self.navigationController pushViewController:registVC animated:YES];
    
}
-(void)forgetPasswordClick
{
    ForgetPasswordViewController *forgetVC = [[ForgetPasswordViewController alloc]init];
    [self.navigationController pushViewController:forgetVC animated:YES];
    
}
-(void)getToken
{
    [AllProjectRequest cookie_tokenWithParameters:nil success:^(id responseObject) {
        
    } failure:^(NSString *error) {
        
    }];
}
-(void)loginClick
{
    __weak __typeof__(self) weakSelf = self;
    [AllProjectRequest loginWithUserPhone:self.phoneTF.text userpwd:self.passwordTF.text userType:@"1" success:^(id responseObject) {
        NSString * massage = responseObject[@"resultMassage"];
        if ([massage isEqualToString:@"登录成功"]) {
            LLUserModel *user = [LLUserModel new];
            user.UserTelephone = self.phoneTF.text ;
            user.passWord = self.passwordTF.text;
            user.UserName = responseObject[@"resultObject"][@"username"];
            user.userid = [NSString stringWithFormat:@"%@",responseObject[@"resultObject"][@"userid"]];
            user.UserIconUrl = responseObject[@"resultObject"][@"userheadpath"];
            user.usernumber = responseObject[@"resultObject"][@"usernumber"];
            [LLUserHandle saveUserDataWith:user];
            [weakSelf.delegate loginSucessWithUser:user];
            HomePageViewController *mainVC = [[HomePageViewController alloc]init];
            BaseNavigationController *baseVC = [[BaseNavigationController alloc]initWithRootViewController:mainVC];
            [UIApplication sharedApplication].keyWindow.rootViewController = baseVC;
        }
        else if([massage isEqualToString:@"请完善信息"])
        {
            LLUserModel *user = [LLUserModel new];
            user.UserTelephone = self.phoneTF.text ;
            user.passWord = self.passwordTF.text;
            [LLUserHandle saveUserDataWith:user];
            LLModificationViewController *modification = [[LLModificationViewController alloc]init];
            [self.navigationController pushViewController:modification animated:YES];
        }else{
            [MBProgressHUD showTheMessage:responseObject[@"resultMassage"] with:nil];
        }
        
    } failure:^(NSString *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}


@end
